package com.thiny.android.app.tnote.adapter;

import android.content.Context;
import android.text.format.DateFormat;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.thiny.android.app.tnote.R;
import com.thiny.android.app.tnote.model.Note;

public class NoteAdapter extends BaseAdapter {

	private Context context;
	private SparseArray<Note> notes;

	public NoteAdapter(Context context, SparseArray<Note> notes) {
		this.context = context;
		this.notes = notes;

		// super(context, list);
	}
 
	@Override
	public int getCount() {
		return notes.size();
	}

	@Override
	public Object getItem(int position) {
		return notes.get(position + 1);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Note note = (Note) getItem(position);
		Holder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.note_item, null);
			holder = new Holder();
			holder.day = (TextView) convertView.findViewById(R.id.note_day);
			holder.title = (TextView) convertView.findViewById(R.id.note_title);
			holder.createTime = (TextView) convertView
					.findViewById(R.id.create_time);
			holder.content = (TextView) convertView
					.findViewById(R.id.note_content);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.day.setText("" + (position + 1));

		if (note == null) {
			holder.title.setText(null);
			holder.createTime.setText(null);
			holder.content.setText(null);
		} else {
//			holder.title.setText(note.getTitle());
			holder.createTime.setText(DateFormat.format("hh:mm",
					note.getCreateTime()));
			holder.content.setText(note.getContent());

		}
		return convertView;
	}


	public class Holder {
		public TextView day;
		public TextView title;
		public TextView createTime;
		public TextView content;
	}

}
